import { useEffect } from 'react'
import { RouterProvider } from 'react-router-dom'
import { useDispatch, useSelector } from 'react-redux'
import { message } from 'antd'
import { setLicenseInfo } from './store/modules/licenseInfoReducer'
import { addPrintTask, removeTaskArray, removeBurnTask, addBurnTask } from './store/modules/taskReducer'
import bus from './utils/eventBus';
import './App.scss';
import router from './router';
import { connect } from './api/index'
import dayjs from "dayjs";
import duration from 'dayjs/plugin/duration'
dayjs.extend(duration)
function App() {
  const dispatch = useDispatch()
  const userInfo = useSelector((state: any) => state.userReducer.userInfo)
  const checkAuthorizeFinished = (data: any) => {
    if (data.code === 200) {
      const duration = dayjs(data.data.endDateTime).diff(dayjs(data.data.startDateTime));
      const year = dayjs.duration(duration).years();
      const months = dayjs.duration(duration).months();
      const days = dayjs.duration(duration).days();
      // `${year}年` || `${months}月` || `${days} 天`
      let effectiveTime = null;
      if (year) {
        effectiveTime = `${year}年`;
      } else if (months) {
        effectiveTime = `${months}月`;
      } else {
        effectiveTime = `${days} 天`;
      }
      dispatch(setLicenseInfo({
        ...data.data,
        effectiveTime,
      }))
    } else {
      dispatch(setLicenseInfo({
        ...data.data,
      }))
      message.error(data.msg);
    }
  }
  const printSuccessStatusFinished = (res: any) => {
    const a = res.currentPage + (res.currentPartNum - 1) * res.totalPage
    const b = res.totalPage * res.partNum
    const progress = Number.isNaN((a / b) * 100) ? 0 : (a / b) * 100;
    dispatch(addPrintTask(res))
    if (progress >= 100) {
      setTimeout(() => {
        dispatch(removeTaskArray(res.taskId))
        message.success('打印成功')
      }, 1500)
    }
  }
  const printErrorStatusFinished = (res: any) => {
    dispatch(removeTaskArray(res))
  }
  const burnStatusFinished = (res: any) => {
    if (res.code === 200) {
      dispatch(addBurnTask(res.data))
    } else {
      dispatch(removeBurnTask())
      message.error(res.msg)
    }
  }
  useEffect(() => {
    window.onPageLoadFinished = () => {
      connect()
    }
    bus.addListener('checkAuthorizeFinished', checkAuthorizeFinished)
    bus.addListener('printSuccessStatusFinished', printSuccessStatusFinished)
    bus.addListener('printErrorStatusFinished', printErrorStatusFinished)
    bus.addListener('burnStatusFinished', burnStatusFinished)
  }, [])
  useEffect(() => {
    return () => {
      bus.removeListener('checkAuthorizeFinished', checkAuthorizeFinished)
      bus.removeListener('printSuccessStatusFinished', printSuccessStatusFinished)
      bus.removeListener('printErrorStatusFinished', printErrorStatusFinished)
      bus.removeListener('burnStatusFinished', burnStatusFinished)
    }
  }, [])
  return (
    <RouterProvider router={router}></RouterProvider>
  )
}

export default App;
